from src.util.Route import *
from src.service import LogManageService


# 获取所有的日志信息(分页 所有信息)
@route("/log/get/handle", methods=['POST'])
def logGetHandle():
    uid = request.environ.get("UID")
    data = request.json
    num = data.get("num")
    size = data.get("size")
    cdn = data.get("cdn")
    if cdn is None:
        cdn = dict()
    page = Page(num=num, size=size)  # Create a Page object
    r = LogManageService.getLogAll(page, 'handle', cdn)
    return R.success(r)


@route("/log/get/sign", methods=['POST'])
def logGetSign():
    uid = request.environ.get("UID")
    data = request.json
    num = data.get("num")
    size = data.get("size")
    cdn = data.get("cdn")
    page = Page(num=num, size=size)  # Create a Page object
    r = LogManageService.getLogAll(page, 'sign', cdn)
    return R.success(r)


# 修改日志信息（除了lid）
@route("/log/update", methods=['POST'])
def logUpdate():
    id = int(request.args.get("id"))
    k: dict = request.json
    k = formatParam(k, ['lId'])
    r = LogManageService.updateLog(id, k)
    if r:
        return R.success(r, "日志信息修改成功")
    else:
        return R.create(400, None, '日志信息修改失败')


# 删除日志
@route("/log/delete", methods=['POST'])
def logDelete():
    lids = request.json
    r = LogManageService.delLog(lids)
    if r:
        return R.success(None, "日志信息删除成功")
    else:
        return R.create(400, None, '日志信息删除失败')


# 日志条件清空
@route("/log/clear", methods=['POST'])
def clearLog():
    body: dict = request.json
    LogManageService.clearLog(body.get('type'), body.get('cdn'))
    return R.success('ok', '操作成功')


@route("/log/export", methods=['POST'])
def exportLog():
    body: dict = request.json
    r = LogManageService.exportLog(body.get('type'), body.get('cdn'))
    return R.success(r, '导出')
